import java.util.Stack;

public class Test_Compare {
    public boolean backspaceCompare(String s, String t) {
        Stack<Character> stack1 = compare(s);
        Stack<Character> stack2 = compare(t);
        while (!stack1.isEmpty() && !stack2.isEmpty()){
            if (stack1.pop() != stack2.pop()){
                return false;
            }
        }
        if (!stack1.isEmpty() || !stack2.isEmpty()){
            return false;
        }
        return true;
    }
    public Stack<Character> compare(String str){
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c != '#'){
                stack.push(c);
            }else{
                if (!stack.isEmpty()){
                    stack.pop();
                }
            }
        }
        return stack;
    }
}
